package tvp.ltdd.fake_call_sms.receiver;

import tvp.ltdd.fake_call_sms.R;
import tvp.ltdd.fake_call_sms.activity.Call;
import tvp.ltdd.fake_call_sms.helper.CallLogHelper;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.WindowManager;

public class CallReceiver extends BroadcastReceiver {

	private String name, number, ringtonepath, phoneId, text;
	private int type, duration;
	private boolean vibrate;
	private NotificationManager myNotificationManager;
	private int notificationIdOne = 111;

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		Bundle extra = intent.getExtras();
		if (extra != null) {
			name = intent.getExtras().getString("name");
			number = intent.getExtras().getString("number");
			ringtonepath = intent.getExtras().getString("ringtonepath");
			phoneId = intent.getExtras().getString("phoneId");
			type = intent.getExtras().getInt("type");
			Log.v("receiver", "" + type);
			vibrate = intent.getExtras().getBoolean("vibrate");
			duration = intent.getExtras().getInt("duration");
		}
		if (type == 2) {
			Intent call = new Intent(context, Call.class);
			call.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			call.putExtra("name", name);
			call.putExtra("number", number);
			call.putExtra("ringtonepath", ringtonepath);
			call.putExtra("phoneId", phoneId);
			call.putExtra("vibrate", vibrate);
			call.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
					+ WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
					+ WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
					+ WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
			context.startActivity(call);
		} else {
			CallLogHelper.insertPlaceholderCall(context.getContentResolver(),
					name, number, type, duration, System.currentTimeMillis());
			if (type == 1)
				displayNotification(context);
		}
	}

	public void setCall(Context context, String name, String number,
			String ringtonepath, String phoneId, long time, int type,
			boolean vibrate, int duration) {
		AlarmManager am = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		Intent intent = new Intent(context, CallReceiver.class);
		intent.putExtra("name", name);
		intent.putExtra("number", number);
		intent.putExtra("ringtonepath", ringtonepath);
		intent.putExtra("phoneId", phoneId);
		intent.putExtra("vibrate", vibrate);
		intent.putExtra("duration", duration);
		intent.putExtra("type", type);
		PendingIntent pi = PendingIntent.getBroadcast(context, 1, intent, 0);
		am.set(AlarmManager.RTC_WAKEUP, time, pi);
	}

	public void displayNotification(Context context) {
		if (name.trim().length() != 0)
			text = name + " " + number;
		else
			text = number;
		Notification mBuilder = new NotificationCompat.Builder(context)
				.setContentText(text).setContentTitle("Missed call")
				.setTicker("Missing call from " + name + " " + number)
				.setSmallIcon(R.drawable.stat_notify_missed_call)
				.setLights(Color.RED, 300, 1000)
				.setDefaults(Notification.FLAG_SHOW_LIGHTS)
				.setVibrate(new long[] { 1000, 1000 }).build();
		myNotificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		myNotificationManager.notify(notificationIdOne, mBuilder);

	}
}
